package com.szqhyxj.youxiangju.biz.equipmentActivation.headIdCard;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.szqhyxj.youxiangju.BaseConsumer;
import com.szqhyxj.youxiangju.biz.core.ImageUploadResult;
import com.szqhyxj.youxiangju.biz.core.ImageUploader;
import com.szqhyxj.youxiangju.biz.realNameAuth.RealNameModel;
import com.szqhyxj.youxiangju.sys.BaseResult;
import com.szqhyxj.youxiangju.sys.util.BitmapCompressFile;

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by leung on 2018/11/1.
 */

public class HeadIdCardPresenter implements HeadIdCardContract.Presenter {
    HeadIdCardContract.View view;
    HeadIdCardModel.HeadIcCardEntity headIcCardEntity;

    public HeadIdCardPresenter(HeadIdCardContract.View view) {
        this.view = view;
        this.headIcCardEntity = new HeadIdCardModel.HeadIcCardEntity();
    }

    @Override
    public void onDestroy() {

    }

    @Override
    public void TakePhotoHeadIdCard(Context context) throws IOException {
        File faceFile = RealNameModel.createImageFile(context);
        this.headIcCardEntity.setHeadIcCardFile(faceFile);
        view.navToTakePhotoHeadIdCard(faceFile);

    }

    @Override
    public void selectFromGalleryForHeadIcCard() {
        view.navToGalleryForHeadIdCard();
    }

    @Override
    public void idcardHeadFromTakePhoto(Context context) throws IOException {
        view.showHeadIdCard(this.headIcCardEntity.getHeadIcCardFile());
        uploadIDCardHand();
    }

    @Override
    public void idcardHeadFromGallery(File file) {
        view.showHeadIdCard(file);
        this.headIcCardEntity.setHeadIcCardFile(file);
        uploadIDCardHand();
    }

    @Override
    public void commitHand() {
        HeadIdCardModel.getuploadHand(this.headIcCardEntity)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<BaseResult>() {
                    @Override
                    public void accept(BaseResult baseResult) throws Exception {
                        if (baseResult.businessLogicSuccess()) {
                            view.CommitSuccess();
                        } else {
                            view.showToast(baseResult.getMessage());
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.showToast(throwable.getMessage());
                        view.CommitFail();
                    }
                });
    }

    private List<Disposable> disposables = new ArrayList<>();

    private void uploadIDCardHand() {
        view.showLoading("正在上传");
        Log.e("aaaaaaaaaaaaaaaa", headIcCardEntity.getHeadIcCardFile() + "");
        File file =BitmapCompressFile.compressFile(this.headIcCardEntity.getHeadIcCardFile());
        Disposable disposable = ImageUploader
                .uploadHandleIDCard(file)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseConsumer<ImageUploadResult>() {

                    @Override
                    protected void error(String code, String msg) {
                        view.dismissLoading();
                    }

                    @Override
                    protected void successAccept(ImageUploadResult baseResult) {
                        view.dismissLoading();
                        headIcCardEntity.setHeadIcCardUrl(baseResult.getFileUrl());
                        view.showIDCardHeadUploadSuccess(baseResult.getFileUrl());
                        view.uploadSuccessHead();
                        file.delete();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) {
                        view.dismissLoading();
                        view.showIDCardHeadUploadFail(throwable.getMessage());
                        file.delete();
                    }
                });
        disposables.add(disposable);
    }


}
